﻿using AntDesign;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using SageKing.Studio.Pages.Base;
using SageKing.Studio.Shared;
using System.Drawing.Drawing2D;

namespace SageKing.Studio.Pages.SageKingMessage.component;

public class ImportDataAttPageModal(
    IMessageService ShowMessage,
    SysSageKingMessageService dataService)
    : ModalCRUDBase<ModelInput, ImportDataAttPage, SysSageKingMessage, ModelInput>
{

    public long messageId { get; set; }
    public List<string> attrNames { get; set; }

    public PageBase<SysSageKingMessage> PageData { get; set; }

    public Action StateHasChanged { get; set; } = () => { };

    public Func<long, Task<List<SysSageKingMessageAttribute>>> QueryChildListFunc { get; set; }


    #region IModalCRUD
    public IModalCRUD<ModelInput, ImportDataAttPage, SysSageKingMessage, ModelInput> MeCrud => this;

    public string TabTitle { get; set; }
    public string Title { get; set; } = "消息属性-快速导入";
    public string ParentName { get; set; }

    public string OkTitle { get; set; } = "导入";

    public string CancelText { get; set; } = "取消";

    public bool Visible { get; set; } = false;

    public bool Loading { get; set; } = false;
    public ModelInput Model { get; set; }
    public ImportDataAttPage Component { get; set; }
    public Error? Error { get; set; }
    public RenderFragment ModalTemplate { get; set; }

    public Task DeleteData(ModelInput row, ModelInput parent)
    {
        throw new NotImplementedException();
    }

    public Task EditOnStatusSwitch(ModelInput row)
    {
        throw new NotImplementedException();
    }

    public async Task HandleOk(MouseEventArgs e)
    {
        try
        {
            Loading = true;
            if (Component != null)
            {
                var result = await Component.Save();

                if (result != 0)
                {
                    Visible = true;
                    Loading = false;

                    if (result == 2)
                    {
                        _ = ShowMessage.WarningAsync("当前消息结构,已有对应属性，无需再次导入！");
                    }
                    return;
                }
                Component.Reset();

            }
            var getParent = PageData.Items.Where(a => a.Id == messageId).FirstOrDefault();
            if (getParent != null)
            {
                getParent.Children = await QueryChildListFunc(messageId);
                await dataService.UpdateVersion(getParent);
            }
            _ = ShowMessage.SuccessAsync($"【{getParent?.Name}】{Title}成功！");
            Visible = false;
            Loading = false;
        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex, $"【{messageId}】{Title}失败");
        }
    }

    public Task<bool> InitModel(SysSageKingMessage row)
    {

        messageId = row.Id;
        attrNames = row.Children.Select(a => a.Name).ToList();

        return Task.FromResult(true);
    }

    #endregion
}
